import { login, getUserProfile, getUserDetailById } from '@/api/user'
import { getToken, setToken } from '@/utils/auth'
// import { resetRouter } from '@/router'

const state = {
  token: getToken() || null,
  userInfo: {}
}

const mutations = {
  setToken(state, newToken) {
    state.token = newToken
    setToken(newToken)
  },
  removeToken() {
    state.token = null
  },
  getUserInfo(state, Newuser) {
    state.userInfo = Newuser
  },
  removeUserInfo(state) {
    state.userInfo = {}
  }
}

const actions = {
  async userLogin(store, parmose) {
    const res = await login(parmose)
    // console.log(context)
    // console.log(res)
    store.commit('setToken', res.data)
  },
  async getUserInfo(store) {
    const res = await getUserProfile()
    const userInfo = await getUserDetailById(res.data.userId)
    console.log(res)
    console.log(userInfo)
    store.commit('getUserInfo', { ...res.data, ...userInfo.data })
  },
  userLoginout(store) {
    store.commit('removeToken')
    store.commit('removeUserInfo')
  }
}

export default {
  namespaced: true,
  state,
  mutations,
  actions
}

